package com.ruoyi.api.user;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.api.user.requestVo.BatterySendRequestVo;
import com.ruoyi.api.user.requestVo.StepMonthlyDataRequestVo;
import com.ruoyi.api.user.requestVo.StepSendRequestVo;
import com.ruoyi.api.user.requestVo.StepWeeklyDataRequestVo;
import com.ruoyi.api.user.response.StepDataResponseVo;
import com.ruoyi.api.user.response.StepListResponseVo;
import com.ruoyi.api.util.DateUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Battery;
import com.ruoyi.user.domain.Machine;
import com.ruoyi.user.domain.Step;
import com.ruoyi.user.domain.bo.StepListResponseBo;
import com.ruoyi.user.service.IBatteryService;
import com.ruoyi.user.service.IMachineService;
import com.ruoyi.user.service.IStepService;

import io.swagger.annotations.ApiOperation;

/**
 * 问题反馈 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/api/step")
public class ApiStepController extends BaseController
{
    private String prefix = "api:step";
	
	@Autowired
	private IStepService stepService;
	@Autowired
	private IMachineService machineService;
	@Autowired
    private IBatteryService batteryService;
	
	/**
	 * 获取运动周数据
	 */
	@PostMapping("/weekly")
	@ResponseBody
	@ApiOperation(value="获取运动周数据", notes="获取运动周数据",produces = "application/json")
	public HashMap<String, Object> weekly(@RequestBody StepWeeklyDataRequestVo weeklyDataRequestVo)
	{		
		StepDataResponseVo vo = new StepDataResponseVo();
		Machine param = new Machine();
		param.setCode(weeklyDataRequestVo.getmCode());
		List<Machine> list = machineService.selectMachineList(param);
		Integer weight = 60;
		Integer totalStep = 0;
		Integer target = 3000;
		try {
			if(list!=null&&list.size()>0){
				Machine ma = list.get(0);
				if(ma.getWeight()!=null&&!"".equals(ma.getWeight())){
					weight = Integer.valueOf(ma.getWeight());
				}
				if(ma.getStepGoal()!=null){
					target = ma.getStepGoal();
				}
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date dBegin = sdf.parse(weeklyDataRequestVo.getBeginTime());
			Date dEnd = sdf.parse(weeklyDataRequestVo.getEndTime());
			List<String> datas = DateUtil.findDates(dBegin, dEnd);
			int count = 0;
			for(String day:datas){
				StepListResponseVo v = new StepListResponseVo();
				StepListResponseBo bo = stepService.selectStepListByDay(day,weeklyDataRequestVo.getmCode());
				v.setDate(day);
				if(bo!=null&&bo.getStep()>0){
					v.setStep(bo.getStep());
				}else{
					v.setStep(0);
				}
				totalStep += v.getStep();
				vo.getList().add(v);
				count++;
			}
			vo.setTotalStep(totalStep);
			vo.setTotalKa(weight,totalStep);
			vo.setPercentage(target*count, totalStep);
		} catch (ParseException e) {
			e.printStackTrace();
			return errorApp("step-0001", "获取周数据异常");
		}
		return successApp(vo);
	}
	
	/**
	 * 获取运动月数据
	 */
	@PostMapping("/monthly")
	@ResponseBody
	@ApiOperation(value="获取运动周数据", notes="获取运动周数据",produces = "application/json")
	public HashMap<String, Object> monthly(@RequestBody StepMonthlyDataRequestVo monthlyDataRequestVo)
	{		
		StepDataResponseVo vo = new StepDataResponseVo();
		Machine param = new Machine();
		param.setCode(monthlyDataRequestVo.getmCode());
		List<Machine> list = machineService.selectMachineList(param);
		Integer weight = 60;//默认60KG
		Integer totalStep = 0;
		Integer target = 10000;//默认10000步
		try {
			if(list!=null&&list.size()>0){
				Machine ma = list.get(0);
				if(ma.getWeight()!=null&&!"".equals(ma.getWeight())){
					weight = Integer.valueOf(ma.getWeight());
				}
				if(ma.getStepGoal()!=null){
					target = ma.getStepGoal();
				}
			}
			// Java8  LocalDate   
			ZoneId zoneId = ZoneId.systemDefault();
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		    LocalDate date = LocalDate.parse(monthlyDataRequestVo.getMonthTime()+"-01");
		    // 该月第一天
		    LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
		    String zdtfirst = firstDay.format(formatter);
		    // 该月最后一天
		    LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
		    String zdtlast = lastDay.format(formatter);
		    
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date dBegin = sdf.parse(zdtfirst);
			Date dEnd = sdf.parse(zdtlast);
			List<String> datas = DateUtil.findDates(dBegin, dEnd);
			int count = 0;
			for(String day:datas){
				StepListResponseVo v = new StepListResponseVo();
				StepListResponseBo bo = stepService.selectStepListByDay(day,monthlyDataRequestVo.getmCode());
				v.setDate(day);
				if(bo!=null&&bo.getStep()>0){
					v.setStep(bo.getStep());
				}else{
					v.setStep(0);
				}
				totalStep += v.getStep();
				vo.getList().add(v);
				count++;
			}
			vo.setTotalStep(totalStep);
			vo.setTotalKa(weight,totalStep);
			vo.setPercentage(target*count, totalStep);
		} catch (ParseException e) {
			e.printStackTrace();
			return errorApp("step-0002", "获取月数据异常");
		}
		return successApp(vo);
	}
	
	/**
	 * 上传步数
	 */
	@PostMapping("/sendStep")
	@ResponseBody
	@ApiOperation(value="上传步数", notes="上传步数",produces = "application/json")
	public HashMap<String, Object> sendStep(@RequestBody StepSendRequestVo requestVo)
	{		
		Step step = new Step();
		step.setImei(requestVo.getmCode());
		step.setPedometer(requestVo.getTotalStep());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			step.setCreateTime(sdf.parse(requestVo.getDateTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			step.setCreateTime(new Date());
		}
		stepService.insertStep(step);
		return successApp("");
	}
	
	/**
	 * 上传电量
	 */
	@PostMapping("/sendBattery")
	@ResponseBody
	@ApiOperation(value="上传电量", notes="上传电量",produces = "application/json")
	public HashMap<String, Object> sendBattery(@RequestBody BatterySendRequestVo requestVo)
	{		
		Battery bb = new Battery();
		bb.setImei(requestVo.getmCode());
		bb.setBattery(requestVo.getBattery());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			bb.setCreateTime(sdf.parse(requestVo.getDateTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			bb.setCreateTime(new Date());
		}
		batteryService.insertBattery(bb);
		return successApp("");
	}

}
